package View;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import FileParser.FileParser;
import Model.Author;
import Model.Headquarters;
import Model.ProgramChair;
import Model.Reviewer;
import Model.SubProgramChair;
import Model.User;

public class UserView extends JFrame implements Observer {
	private static final long serialVersionUID = 1L;  //to get rid of warning
	private static final int WIDTH = 850;
	private static final int HEIGHT = 600;
	
	private static final int TOP_PANEL_HEIGHT = 2;
	private static final int BOT_PANEL_HEIGHT = 14;
	private static final int FRAME_HEIGHT = 16;
	private static final String comboBoxItems[] = { "Headquarters", "PC", "SubPC", "Reviewer", "Author", "User" };
	private final JFrame mainFrame;
	private User user;
	private Headquarters hq;
	private ProgramChair pcr;
	private SubProgramChair spc;
	private HeadquartersView hqView;
	private ProgramChairView pcView;
	private SPCView spcView;
	private FileParser fp;
	private JTextField text;
	private JComboBox roleBox;
	private JComboBox conferenceCombo;
	
	public UserView(User user, FileParser a_fp) {
		mainFrame = new JFrame("Login");
		fp = a_fp;
		this.user = user;
	}
	
	public void start() {
		mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		mainFrame.setSize(WIDTH, HEIGHT);
		mainFrame.setResizable(false);
		mainFrame.setLocationRelativeTo(null);		//set in the middle of the screen
		mainFrame.setLayout(new BorderLayout());
		mainFrame.add(createTopPanel(), BorderLayout.NORTH);  //use the enum Roles for the role string
		mainFrame.add(createLeftBotPanel(), BorderLayout.WEST);
		mainFrame.add(createRightBotPanel(), BorderLayout.EAST);
		mainFrame.setVisible(true);
	}
	
	/**
	 * Creates the top panel that contains name, conference, & role info.
	 */
	private JPanel createTopPanel() {
		JPanel topPnl = new JPanel();
		topPnl.setBackground(Color.LIGHT_GRAY);	
		topPnl.setPreferredSize(new Dimension(WIDTH, TOP_PANEL_HEIGHT * HEIGHT / FRAME_HEIGHT));	//set this panel to 2/16 of the JFrame
		//topPnl.setLayout(new GridLayout(3,1,5,5)); //3 rows, 1 columns, hgap = 5, vgap = 5
		
		

		JLabel nameLbl 		= new JLabel("WOLVERINES", JLabel.CENTER);      //create labels for user info 
		topPnl.add(nameLbl);
		add(topPnl);	
		return topPnl;
	}

	private JPanel createLeftBotPanel() {
		JPanel botLftPnl = new JPanel();
		botLftPnl.setBackground(Color.WHITE);
		botLftPnl.setPreferredSize(new Dimension(WIDTH / 2, BOT_PANEL_HEIGHT * HEIGHT / FRAME_HEIGHT));  //set this panel to 3/16 of the JFrame
		FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
		layout.setVgap(30);										//vertical gap for the buttons
		layout.setHgap(25);										//horizontal gap for the buttons
		botLftPnl.setLayout(layout);
		
		
		JLabel loginLbl = new JLabel("LOGIN", JLabel.CENTER);	
		loginLbl.setPreferredSize(new Dimension(WIDTH / 2, 100));
		JLabel userNameLbl = new JLabel("User Name: ");
		userNameLbl.setPreferredSize(new Dimension(100, 20));
		 text = new JTextField("", 20);
		
		JLabel conferenceLbl = new JLabel("Conference: ");
		conferenceLbl.setPreferredSize(new Dimension(100, 20));
		conferenceCombo = new JComboBox(user.getListOfConferences());
		conferenceCombo.setPreferredSize(new Dimension(225, 20));
		conferenceCombo.setEnabled(false);
		
		JLabel roleLbl = new JLabel("User Role:");
		roleLbl.setPreferredSize(new Dimension(100, 20));
		roleBox = new JComboBox(comboBoxItems);
		roleBox.setPreferredSize(new Dimension(225, 20));
		JButton loginBut = new JButton("Login");
		roleBox.addActionListener(new ActionListener(){
		    public void actionPerformed(final ActionEvent the_event){
	        	if(roleBox.getSelectedItem().equals(comboBoxItems[0]) ||
	        			roleBox.getSelectedItem().equals(comboBoxItems[5])){
	        		conferenceCombo.setEnabled(false);
	           	} else {
	        		conferenceCombo.setEnabled(true);
	        	}
	        }
	    });
		
		loginBut.addActionListener(new ActionListener()
	      {
			
	        public void actionPerformed(final ActionEvent the_event){
	        	String name = text.getText();
				String role = (String) roleBox.getSelectedItem();
				String conf = (String) conferenceCombo.getSelectedItem();
	        	boolean exist = false;
	        	if(roleBox.getSelectedItem().equals(comboBoxItems[0]) ||
	        			roleBox.getSelectedItem().equals(comboBoxItems[5])){
	        		//conferenceCombo.setEnabled(false);
	        		System.out.println("It went here");
	        		exist = user.login(role, name);
	        	} else {
	        		//conferenceCombo.setEnabled(true);
	        		exist = user.login(name, role, conf);
	        	}
	        	if(exist) {
	        		mainFrame.dispose();
	        		if(roleBox.getSelectedItem().equals(comboBoxItems[0])) {
	        			hq = new Headquarters(fp, name);
	        			hqView = new HeadquartersView(fp, hq);
	        			hqView.start();
	   				} else if(roleBox.getSelectedItem().equals(comboBoxItems[1])){
	        			pcr = new ProgramChair(fp, name, fp.getConference(conf));
	        			pcView = new ProgramChairView(pcr);
	        			pcView.start();
			
	        		} else if(roleBox.getSelectedItem().equals(comboBoxItems[2])) {
	        			spc = new SubProgramChair(fp, name, fp.getConference(conf));
	        			spcView = new SPCView(spc);
	        			spcView.start();
	        		} else if(roleBox.getSelectedItem().equals(comboBoxItems[3])) {
	   					Reviewer reviewer = new Reviewer(fp, name, fp.getConference(conf));
	        			ReviewerView aView = new ReviewerView(reviewer);
	        			aView.start();
	        		
	        					
	   				} else if(roleBox.getSelectedItem().equals(comboBoxItems[4])) {
	        			Author auth = new Author(fp, name, fp.getConference(conf));
	        			AuthorView aView = new AuthorView(auth);
	        			aView.start();
	   				} else if(roleBox.getSelectedItem().equals(comboBoxItems[5])) {
	   					JOptionPane.showMessageDialog(null, "Login Sucessful");
	        			
	   				}
	        		JOptionPane.showMessageDialog(null, role);
	        	} else {
	        		JOptionPane.showMessageDialog(null, "Error" );
	        	}
	        }
	      });
		JLabel blankLbl = new JLabel();
		blankLbl.setPreferredSize(new Dimension(150, 20));
		
		botLftPnl.add(loginLbl);
		botLftPnl.add(userNameLbl);
		botLftPnl.add(text);
		botLftPnl.add(conferenceLbl);
		botLftPnl.add(conferenceCombo);
		botLftPnl.add(roleLbl);
		botLftPnl.add(roleBox);
		botLftPnl.add(blankLbl);
		botLftPnl.add(loginBut);

		add(botLftPnl);
		return botLftPnl;
	}
	
	/**
	 * @return
	 */
	private JPanel createRightBotPanel() {
		JPanel botRgtPnl = new JPanel();
		botRgtPnl.setBackground(Color.WHITE);
		botRgtPnl.setPreferredSize(new Dimension(WIDTH / 2, BOT_PANEL_HEIGHT * HEIGHT / FRAME_HEIGHT));  //set this panel to 3/16 of the JFrame
		FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
		layout.setVgap(30);										//vertical gap for the buttons
		layout.setHgap(25);										//horizontal gap for the buttons
		botRgtPnl.setLayout(layout);
		
		
		JLabel signUpLbl = new JLabel("SIGN UP", JLabel.CENTER);	
		signUpLbl.setPreferredSize(new Dimension(WIDTH / 2, 100));
		JLabel userName = new JLabel("Name:", JLabel.RIGHT);
		userName.setPreferredSize(new Dimension(75,20));
		//userName.setForeground(Color.RED);
		final JTextField userText = new JTextField("Enter your first and last names", 20);
		
		JLabel emailLbl = new JLabel("Email:", JLabel.RIGHT);				
		emailLbl.setPreferredSize(new Dimension(75,20));
		final JTextField emailText = new JTextField("Enter your email address", 20);
		JLabel phoneLbl = new JLabel("Phone:", JLabel.RIGHT);
		phoneLbl.setPreferredSize(new Dimension(75,20));
		final JTextField phoneText = new JTextField("Enter your phone number", 20);
			
		JButton signUpBut = new JButton("Sign Up");
		signUpBut.addActionListener(new ActionListener(){
		    public void actionPerformed(final ActionEvent the_event){
		    	if(userText.getText().isEmpty() || emailText.getText().isEmpty() || phoneText.getText().isEmpty()){
		    		JOptionPane.showMessageDialog(null, "One of the text field is blank");
		    	}
		    	User newUser = new User(fp);
		    	newUser.register(userText.getText(), emailText.getText(), phoneText.getText());
	        }
	    });
		JLabel blankRoleLbl = new JLabel();
		blankRoleLbl.setPreferredSize(new Dimension(140, blankRoleLbl.getHeight()));
				
		botRgtPnl.add(signUpLbl);
		botRgtPnl.add(userName);
		botRgtPnl.add(userText);

		botRgtPnl.add(emailLbl);
		botRgtPnl.add(emailText);

		botRgtPnl.add(phoneLbl);
		botRgtPnl.add(phoneText);
		//
		botRgtPnl.add(blankRoleLbl);
		botRgtPnl.add(signUpBut);

		add(botRgtPnl);
		return botRgtPnl;
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		
	}
	
	
	
}
